package cate.common.table.rank;

import cate.common.table.EExcel;
import cate.common.table.ESheet;
import cate.common.table.rank.row.IRewardRankRow;
import cate.common.table.rank.sheet.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RankExcel extends EExcel {
    private final Map<Integer, IRewardRankSheet<? extends IRewardRankRow>> rankMap = new HashMap<>();

    public RankBaseSheet base = new RankBaseSheet();

    public RankLevelSheet level = new RankLevelSheet();

    public RankAchievementSheet adventure = new RankAchievementSheet();

    public RankBrightScoreSheet bright = new RankBrightScoreSheet();

    public RankDarkScoreSheet dark = new RankDarkScoreSheet();

    public RankEarthScoreSheet earth = new RankEarthScoreSheet();

    public RankFireScoreSheet fire = new RankFireScoreSheet();

    public RankPowerSheet power = new RankPowerSheet();

    public RankWaterScoreSheet water = new RankWaterScoreSheet();

    public RankWoodScoreSheet wood = new RankWoodScoreSheet();

    public RankExcel(){
        super();
        rankMap.put(level.getRankId(), level);
        rankMap.put(adventure.getRankId(), adventure);
        rankMap.put(water.getRankId(), water);
        rankMap.put(fire.getRankId(), fire);
        rankMap.put(wood.getRankId(), wood);
        rankMap.put(earth.getRankId(), earth);
        rankMap.put(bright.getRankId(), bright);
        rankMap.put(dark.getRankId(), dark);
        rankMap.put(power.getRankId(), power);
    }

    @Override
    public String getFileName() {
        return "排行榜.xlsx";
    }

    @Override
    public ESheet[] getSheets() {
        return new ESheet[]{base, level, adventure, bright, dark, earth, fire, power, water, wood};
    }

    public long getLeastValue(int rankId){
        IRewardRankSheet<? extends IRewardRankRow> sheet = rankMap.get(rankId);
        if(sheet == null || sheet.getRewardRows().isEmpty()){
            return 0;
        }
        return sheet.getRewardRows().get(0).getRewardValue();
    }

    public long canGetRewardSize(int rankId, long value){
        return getReachRows(rankId, value).size();
    }

    public boolean canRedPoint(int rankId, int rewardId, int level) {
        IRewardRankRow row = getRewardRow(rankId, rewardId);
        return row != null && row.getLevelLimit() <= level;
    }

    public IRewardRankRow getRewardRow(int rankId, int rewardId){
        IRewardRankSheet<? extends IRewardRankRow> sheet = rankMap.get(rankId);
        if(sheet == null){
            return null;
        }
        List<? extends IRewardRankRow> list = sheet.getRewardRows();
        for(IRewardRankRow rewardRankRow : list){
            if(rewardRankRow.getRewardId() == rewardId){
                return rewardRankRow;
            }
        }
        return null;
    }



    public List<IRewardRankRow> getReachRows(int rankId, long value){
        List<IRewardRankRow> list = new ArrayList<>();
        IRewardRankSheet<? extends IRewardRankRow> sheet = rankMap.get(rankId);
        if(sheet == null){
            return list;
        }
        return sheet.getRewardRows().stream().filter(e -> value >= e.getRewardValue()).collect(Collectors.toList());
    }

    public boolean hasReward(int rankId){
        return base.get(rankId) != null && base.get(rankId).canGetReward;
    }
}
